<?php
/**
 * Created by PhpStorm.
 * User: liugang <liugang3@xin.com>
 * Date: 2019-04-24
 * Time: 07:13
 */

namespace App\Http\Controllers\Api\V1;


use App\Http\Controllers\Api\ApiController;
use App\Models\OwnerBank;
use Illuminate\Http\Request;
use Validator;


class OwnerBankController extends ApiController
{
    /**
     * 银行卡信息列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function bankList(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'owners_uuid' => 'required|size:36',
                'page' => 'required|integer',
                'limit' => 'required|integer'
            ]);

            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            $res = ownerBank::select(['*'])
                ->where('owners_uuid', $request->get('owners_uuid'))
                ->orderBy('id', 'desc')
                ->paginate($request->get('limit', self::DEFAULT_PAGE_LIMIT))
                ->toArray();
            if (empty($res)) {
                return $this->success();
            }

            return $this->table($res['data'], $res['total']);
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 添加银行卡操作
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addBank(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'owners_uuid' => 'required|size:36',
                'owners_name' => 'required',
//                'driver_phone' => 'required|size:11',
                'bank_card_no' => 'required',
                'bank_branch' => 'required',
            ]);

            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            $data['owners_uuid'] = $request->get('owners_uuid');
            $data['owners_name'] = $request->get('owners_name');
            $data['bank_card_no'] = $request->get('bank_card_no');
            $data['bank_branch'] = $request->get('bank_branch');

            $data['owners_phone'] = $request->get('owners_phone', '');
            $data['bank_pictures'] = $request->get('bank_pictures', '');

            OwnerBank::insert($data);

            return $this->success('success');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 编辑银行卡操作
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editBank(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'id' => 'required|integer',
                'owners_uuid' => 'required|size:36',
                'owners_name' => 'required',
//                'driver_phone' => 'required|size:11',
                'bank_card_no' => 'required',
                'bank_branch' => 'required',
            ]);

            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            $model = OwnerBank::where('id', $request->get('id'))
                ->where('owners_uuid', $request->get('owners_uuid'))
                ->first();
            if (is_null($model)) {
                throw new \Exception('没有找到该司机的银行卡');
            }

            $model->owners_name = $request->get('owners_name');
            $model->bank_card_no = $request->get('bank_card_no');
            $model->bank_branch = $request->get('bank_branch');
            $model->updated_at = date('Y-m-d H:i:s');

            $model->owners_phone = $request->get('owners_phone', '');
            $model->bank_pictures = $request->get('bank_pictures', '');

            $model->save();

            return $this->success('success');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 删除银行卡操作
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteBank(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'owners_uuid' => 'required|size:36',
                'bank_card_no' => 'required',
            ]);

            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            OwnerBank::where('owners_uuid', $request->get('owners_uuid'))
                ->where('bank_card_no', $request->get('bank_card_no'))
                ->delete();

            return $this->success('success');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

}